//
//  XQUserController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQUserController.h"
#import "XQTopCardView.h"
#import "XQBaseTableListArrowView.h"
#import "XQUserSettingController.h"

@interface XQUserController ()
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *mainView;
@property (nonatomic,strong) XQTopCardView *cardView;
@property (nonatomic,strong) XQBaseTableListArrowView *tableView;
@property (nonatomic,strong) NSMutableDictionary *dataDic;
@end

@implementation XQUserController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScrollView];
    [self makeUI];
    [self makeConstraints];
    
    
    [XQHUD showLoadingInView:self.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self refreshData];
}

- (void)makeUI {
    self.view.backgroundColor = kCardColor;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.mainView];
    [self.view addSubview:self.nameLabel];
    [self.mainView addSubview:self.cardView];
    [self.mainView addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].statusBarHeight + 9.5);
        make.left.equalTo(@24);
    }];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].topHeight + 8);
        make.left.right.equalTo(@0);
        make.bottom.offset(-[UIScreen mainScreen].bottomHeight - 49);
    }];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        make.width.mas_equalTo([UIScreen mainScreen].width);
    }];
    
    [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.right.equalTo(@0);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cardView.mas_bottom).offset(20);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
}

- (void)initScrollView {
    MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    self.scrollView.mj_header = refreshHeader;
}

- (void)refreshData {
    @weakify(self)
    [XQAFNetworking getWithUrl:[XQNetManager getAbsolutePath:@"/direct/nica"] pars:@{} callback:^(_Bool success, id  _Nonnull responseObject, NSError * _Nonnull error) {
        @strongify(self);
        [self.scrollView.mj_header endRefreshing];
        [XQHUD hideLoadindInView:self.view];
        if ([responseObject[@"concept"] integerValue] == 0) {
            self.dataDic = responseObject[@"accomplish"];
            [self updateTopData];
            self.tableView.dataSource = self.dataDic[@"rakuten"];
        }
    }];
}

#pragma mark - private

- (void)cellDidClickWithName:(NSString *)name {
    [XQRouter jumpFromController:self url:name];
}

- (void)updateTopData {
    NSDictionary *userInfo = self.dataDic[@"userInfo"];
    
//    [self.cardView setImageName:userInfo[@"asset"]];
    [self.cardView setNick:userInfo[@"userphone"]];
}

#pragma mark - lazy

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#0E1D31"];
        _nameLabel.font = [UIFont boldSystemFontOfSize:18];
        _nameLabel.text = @"Hello,user!";
    }
    return _nameLabel;
}

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
    }
    return _mainView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
    }
    return  _scrollView;
}


- (XQTopCardView *)cardView {
    if (!_cardView) {
        _cardView = [[XQTopCardView alloc] init];
        [_cardView setNickFont:18];
        [_cardView setNickColor:[UIColor whiteColor]];
        [_cardView setPadding:14];
        [_cardView setAvatarImageName:@"avatar"];
    }
    return _cardView;
}

- (XQBaseTableListArrowView *)tableView {
    if (!_tableView) {
        _tableView = [XQBaseTableListArrowView new];
        @weakify(self);
        _tableView.cellClickHandle = ^(NSString * _Nonnull title) {
            @strongify(self);
            [self cellDidClickWithName:title];
        };
    }
    return _tableView;
}

@end
